import {
    Controller,
    Get,
    Post,
    Body,
    Patch,
    Param,
    Delete,
    UseGuards,
    Request,
    NotFoundException,
    ForbiddenException,
    HttpStatus
} from '@nestjs/common';
import { UsersService } from './users.service';
import { UpdateUserDto, ChangePasswordDto } from './dto/update-user.dto';
import { JwtAuthGuard } from '../auth/guards/jwt-auth.guard';
import { RolesGuard } from '../auth/guards/roles.guard';
import { Roles } from '../auth/decorators/roles.decorator';
import { Public } from '../auth/decorators/public.decorator';

@Controller('users')
@UseGuards(JwtAuthGuard, RolesGuard)
export class UsersController {
    constructor(private readonly usersService: UsersService) { }

    @Get('profile')
    async getProfile(@Request() req) {
        try {
            const user = await this.usersService.findById(req.user.userId);
            return {
                status: HttpStatus.OK,
                message: '获取用户资料成功',
                data: user
            };
        } catch (error) {
            if (error instanceof NotFoundException) {
                throw error;
            }
            throw new ForbiddenException('获取用户资料失败');
        }
    }

    @Patch('profile')
    async updateProfile(@Request() req, @Body() updateUserDto: UpdateUserDto) {
        try {
            const updatedUser = await this.usersService.update(req.user.userId, updateUserDto);
            return {
                status: HttpStatus.OK,
                message: '更新用户资料成功',
                data: updatedUser
            };
        } catch (error) {
            throw new ForbiddenException('更新用户资料失败: ' + error.message);
        }
    }

    @Post('change-password')
    async changePassword(@Request() req, @Body() changePasswordDto: ChangePasswordDto) {
        try {
            await this.usersService.changePassword(req.user.userId, changePasswordDto);
            return {
                status: HttpStatus.OK,
                message: '密码修改成功',
            };
        } catch (error) {
            throw new ForbiddenException('密码修改失败: ' + error.message);
        }
    }

    @Get('settings')
    async getUserSettings(@Request() req) {
        try {
            const settings = await this.usersService.getUserSettings(req.user.userId);
            return {
                status: HttpStatus.OK,
                message: '获取用户设置成功',
                data: settings
            };
        } catch (error) {
            throw new ForbiddenException('获取用户设置失败');
        }
    }

    @Patch('settings')
    async updateUserSettings(@Request() req, @Body() settingsData: any) {
        try {
            const settings = await this.usersService.updateUserSettings(req.user.userId, settingsData);
            return {
                status: HttpStatus.OK,
                message: '更新用户设置成功',
                data: settings
            };
        } catch (error) {
            throw new ForbiddenException('更新用户设置失败');
        }
    }

    @Get('artworks')
    async getUserArtworks(@Request() req) {
        try {
            const artworks = await this.usersService.getUserArtworks(req.user.userId);
            return {
                status: HttpStatus.OK,
                message: '获取用户作品成功',
                data: artworks
            };
        } catch (error) {
            throw new ForbiddenException('获取用户作品失败');
        }
    }

    @Post('favorites/:artworkId')
    async favoriteArtwork(@Request() req, @Param('artworkId') artworkId: string) {
        try {
            await this.usersService.favoriteArtwork(req.user.userId, artworkId);
            return {
                status: HttpStatus.OK,
                message: '收藏作品成功',
            };
        } catch (error) {
            throw new ForbiddenException('收藏作品失败: ' + error.message);
        }
    }

    @Delete('favorites/:artworkId')
    async unfavoriteArtwork(@Request() req, @Param('artworkId') artworkId: string) {
        try {
            await this.usersService.unfavoriteArtwork(req.user.userId, artworkId);
            return {
                status: HttpStatus.OK,
                message: '取消收藏成功',
            };
        } catch (error) {
            throw new ForbiddenException('取消收藏失败: ' + error.message);
        }
    }

    @Get('favorites')
    async getFavoritedArtworks(@Request() req) {
        try {
            const artworks = await this.usersService.getFavoritedArtworks(req.user.userId);
            return {
                status: HttpStatus.OK,
                message: '获取收藏作品成功',
                data: artworks
            };
        } catch (error) {
            throw new ForbiddenException('获取收藏作品失败');
        }
    }

    // 管理员接口
    @Get()
    @Roles('admin')
    async findAllUsers() {
        const users = await this.usersService.findAll();
        return {
            status: HttpStatus.OK,
            message: '获取所有用户成功',
            data: users
        };
    }

    @Get(':id')
    @Roles('admin')
    async findOne(@Param('id') id: string) {
        try {
            const user = await this.usersService.findById(id);
            return {
                status: HttpStatus.OK,
                message: '获取用户成功',
                data: user
            };
        } catch (error) {
            if (error instanceof NotFoundException) {
                throw error;
            }
            throw new ForbiddenException('获取用户失败');
        }
    }

    @Delete(':id')
    @Roles('admin')
    async remove(@Param('id') id: string) {
        try {
            await this.usersService.remove(id);
            return {
                status: HttpStatus.OK,
                message: '删除用户成功',
            };
        } catch (error) {
            throw new ForbiddenException('删除用户失败: ' + error.message);
        }
    }
} 